package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Article;
import util.DbUtil;

public class ArticleDao {
  
	public void add(Article article) {
		
		try {
			Connection conn=DbUtil.getConn();
			//System.out.println("数据库连接成功！");
			String sql = "insert into Article(id, content, posttime, ip) values(?, ?, ?, ?)";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, article.getId());
			pst.setString(2, article.getContent());
			pst.setDate(3, new java.sql.Date(article.getPostTime().getTime()));
			pst.setString(4, article.getIp());
	
			
			pst.executeUpdate();
			
			pst.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void update(Article article) {
		try {
			//System.out.println("数据库连接成功！");
			Connection conn=DbUtil.getConn();
			String sql = "update article set content=?,postTime=?,ip=? where id=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, article.getContent());
			pst.setDate(2, new java.sql.Date(article.getPostTime().getTime()));
			pst.setString(3, article.getIp());
			pst.setString(4, article.getId());
			
			pst.executeUpdate();
			
			pst.close();
			
			
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(String id) {
		try {
			Connection conn=DbUtil.getConn();
			//System.out.println("数据库连接成功！");
			String sql = "delete from article where id=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			pst.executeUpdate();
			pst.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public Article findById(String id) {
		Article article = null;
		try {
			Connection conn=DbUtil.getConn();
			String sql = "select * from article where id = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				article = new Article();
				article.setId(rs.getString("id"));
				article.setContent(rs.getString("content"));
				article.setIp(rs.getString("ip"));
				article.setPostTime(rs.getDate("postTime"));
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return article;
	}
	
	public List<Article> findAll(){
		List<Article> articles = new ArrayList<Article>();
		try{
			Connection conn=DbUtil.getConn();
			String sql = "select * from article";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			
			while(rs.next()){
				Article article=new Article();
				article.setId(rs.getString(1));
				article.setContent(rs.getString(2));
				article.setPostTime(rs.getDate(3));
				article.setIp(rs.getString(4));
				
				articles.add(article);
			}
			
			rs.close();
			
			ps.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return articles;
	}

}
